﻿using UnityEngine;
using System.Collections;

public class WorldExtensionBattleStageScene : LTEventListenerC {
	public GameObject[] stageRoot;
	public GameObject[] stages;
	public GameObject[] stages_Verteron;
	public GameObject[] stages_Theomobos;

	public UILabel stageMaxCountLabel;
	public UILabel stageOpenedCountLabel;

	public GameObject[] stageScrollList;

	public static int selectedAreaId;
	public static int selectedAreaIndex;

	public GameObject stageObjects;
	public GameObject titleObject;

	public UILabel areaTitleLabel;
	public UILabel areaSubTitleLabel;

	int currentIndex = 0;

	public GameObject[] regionAnimatedTextObject;

	TweenAlpha tweenAlphaTitle;
	TweenAlpha tweenAlphaStages;
	TweenPosition tweenPositionRoot;
	TweenPosition[] tweenPosionList = new TweenPosition[10];
	TweenRotation[] tweenRotationList = new TweenRotation[10];
	public GameObject verteron_3dText;
	public GameObject Theomobos_3dText;
    public GameObject Eltenen_3dText;

    public GameObject expansionWarStartButtonObject;

	public override void AwakeScene ()
	{
		base.AwakeScene ();
		
		hasLeftTopMenu = true;
		hasBackButton = true;
		//SetActiveHelpButton(true);

		//BGM 재생 임시 적용
		if( UserData.Instance.userModel.regions.regionMap_Dic.Count == 1 )
            BGM = "BGM_Map_Berteron";
        else if (UserData.Instance.userModel.regions.regionMap_Dic.Count == 2)
            BGM = "BGM_Map_Eltenen";
        else if (UserData.Instance.userModel.regions.regionMap_Dic.Count == 2)
            BGM = "BGM_Map_Teobomos";
	}

	int lastFinishedStageIndex = 0;
	// Use this for initialization
	public override void StartScene ()
	{
		base.StartScene ();

		areaTitleLabel.text = TextController.GetStringWithId(string.Format("AreaTitleRegion{0}", selectedAreaId));
		areaSubTitleLabel.text = string.Format("{0} {1}", TextController.GetStringWithId(string.Format("region{0}", selectedAreaId)), TextController.GetStringWithId("uiMainExpansion"));

		if( WorldSceneManager.GetSelectedAreaId() == 101 )
		{
			BGM = "BGM_Map_Berteron";
//			stages = stages_Verteron;
			verteron_3dText.SetActive(true);
		}
		else if( WorldSceneManager.GetSelectedAreaId() == 102 )
		{
//			stages = stages_Theomobos;
            Eltenen_3dText.SetActive(true);
		}
        else if (WorldSceneManager.GetSelectedAreaId() == 103)
        {
            //stages = stages_Theomobos;
            Theomobos_3dText.SetActive(true);
        }

		if( UserData.Instance.userModel.regions.expansionWarId == 0 )
		{
			chooseIndex = lastFinishedStageIndex = 0;
		}
		else if( UserData.Instance.userModel.regions.expansionWarId == selectedAreaId )
		{
			chooseIndex = lastFinishedStageIndex = UserData.Instance.userModel.regions.expansionWarStep-1;
		}

		if( stageScrollList[chooseIndex].transform.FindChild("01") != null )		Debug.LogError("chooseIndex - " + chooseIndex);

//		tweenPositionRoot = TweenPosition.Begin(stageRoot, 0.5f, new Vector3(stageRoot.transform.localPosition.x, stageRoot.transform.localPosition.y, 2*currentIndex-5f));
//		EventDelegate.Add(tweenPositionRoot.onFinished, OnFinishedMoveStageRoot);
		tweenAlphaTitle = TweenAlpha.Begin(titleObject, 1.5f, 1f);
		EventDelegate.Add(tweenAlphaTitle.onFinished, OnFinishedAlphaTtile);

		for(int i=0; i<lastFinishedStageIndex; i++)
		{
			stageScrollList[i].transform.FindChild("01").gameObject.SetActive(true);
			stageScrollList[i].transform.FindChild("02").gameObject.SetActive(false);
			stageScrollList[i].transform.FindChild("03").gameObject.SetActive(false);
			stageScrollList[i].transform.FindChild("04").gameObject.SetActive(false);
		}
		if( stageScrollList[chooseIndex].transform.FindChild("01") != null )
			stageScrollList[chooseIndex].transform.FindChild("01").gameObject.SetActive(false);
		if( stageScrollList[chooseIndex].transform.FindChild("02") != null )
			stageScrollList[chooseIndex].transform.FindChild("02").gameObject.SetActive(true);
		if( stageScrollList[chooseIndex].transform.FindChild("03") != null )
			stageScrollList[chooseIndex].transform.FindChild("03").gameObject.SetActive(false);
		if( stageScrollList[chooseIndex].transform.FindChild("04") != null )
			stageScrollList[chooseIndex].transform.FindChild("04").gameObject.SetActive(false);

		if( lastFinishedStageIndex < 5 ) 
			stageScrollList[0].transform.parent.parent.gameObject.GetComponent<UIScrollView>().contentPivot = UIWidget.Pivot.Top;
		else if( lastFinishedStageIndex > 4 && lastFinishedStageIndex < 7 ) 
			stageScrollList[0].transform.parent.parent.gameObject.GetComponent<UIScrollView>().contentPivot = UIWidget.Pivot.Center;
		else
			stageScrollList[0].transform.parent.parent.gameObject.GetComponent<UIScrollView>().contentPivot = UIWidget.Pivot.Bottom;

		stageScrollList[0].transform.parent.parent.gameObject.GetComponent<UIScrollView>().ResetPosition();

		stageOpenedCountLabel.text = (UserData.Instance.userModel.regions.expansionWarStep==0?1:(int)UserData.Instance.userModel.regions.expansionWarStep).ToString();

        //if( WorldSceneManager.GetSelectedAreaId() == 102 && UserData.Instance.userModel.regions.expansionWarStep == 10 )
        //{
        //    Popup_OneButton po = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;
        //    po.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiCommonTextwarning"), TextController.GetStringWithId("uiCommonTextTobeContinue"));
        //    po.SetEventOK(gameObject, "BackButton");
        //    return;
        //}
	}

	public void OnFinishedAlphaTtile()
	{
		for(int i=0; i<10; i++)
		{
			if( i < 9 )
			{
				stages[i].transform.FindChild((i+1)+"_1").FindChild("Text").GetComponent<UILabel>().text = TextController.GetStringWithId(string.Format("expansionWar{0}0{1}", selectedAreaIndex, (i+1).ToString()));
				stages[i].transform.FindChild((i+1)+"_2").FindChild("Text").GetComponent<UILabel>().text = TextController.GetStringWithId(string.Format("expansionWar{0}0{1}", selectedAreaIndex, (i+1).ToString()));
			}
			else
			{
				stages[i].transform.FindChild("Boss_1").FindChild("Text").GetComponent<UILabel>().text = TextController.GetStringWithId(string.Format("expansionWar{0}{1}", selectedAreaIndex, (i+1).ToString()));
				DExtendWar expansionRewardInfo = new DExtendWar();
				foreach(DExtendWar info in GameData.Inst.extendWar)
				{
					if( info.regionID == WorldSceneManager.GetSelectedAreaId() )
					{
						if( info.stage == 10 )
						{
							expansionRewardInfo = info;
							break;
						}
					}
				}
				stages[i].GetComponentInChildren<DispatchItemCell>().itemName.text = TextController.GetItemName(expansionRewardInfo.value);
				stages[i].GetComponentInChildren<DispatchItemCell>().itemImage.spriteName = TextController.GetItemDefine(expansionRewardInfo.value);
                WeaponAtlasController.SetWeaponSprite(stages[i].GetComponentInChildren<DispatchItemCell>().itemImage, expansionRewardInfo.value);
                stages[i].GetComponentInChildren<DispatchItemCell>().rareStar[GameData.Inst.GetItemRarity(expansionRewardInfo.value)-1].SetActive(true);
			}
		}
		tweenAlphaTitle = TweenAlpha.Begin(titleObject, 1.5f, 0f);
		tweenAlphaTitle.RemoveOnFinished(new EventDelegate(OnFinishedAlphaTtile));
		tweenAlphaStages = TweenAlpha.Begin(stageObjects, 1.5f, 1f);
		MoveToStagePanel();
	}

	public override void BackButton()
	{
		base.BackButton();

		SceneController.LoadLevel("World");
	}

	int chooseIndex=0;
	public override void OnEvent (int eventID)
	{
		base.OnEvent (eventID);

		if( eventID >= lastFinishedStageIndex + 1 )
			return;

		if( chooseIndex < lastFinishedStageIndex )
		{
			if( stageScrollList[chooseIndex].transform.FindChild("01") != null )
				stageScrollList[chooseIndex].transform.FindChild("01").gameObject.SetActive(true);
			if( stageScrollList[chooseIndex].transform.FindChild("02") != null )
				stageScrollList[chooseIndex].transform.FindChild("02").gameObject.SetActive(false);
			if( stageScrollList[chooseIndex].transform.FindChild("03") != null )
				stageScrollList[chooseIndex].transform.FindChild("03").gameObject.SetActive(false);
			if( stageScrollList[chooseIndex].transform.FindChild("04") != null )
				stageScrollList[chooseIndex].transform.FindChild("04").gameObject.SetActive(false);
		}
		else
		{
			if( chooseIndex < 9 )
			{
				if( stageScrollList[chooseIndex].transform.FindChild("01") != null )
					stageScrollList[chooseIndex].transform.FindChild("01").gameObject.SetActive(false);
				if( stageScrollList[chooseIndex].transform.FindChild("02") != null )
					stageScrollList[chooseIndex].transform.FindChild("02").gameObject.SetActive(false);
				if( stageScrollList[chooseIndex].transform.FindChild("03") != null )
					stageScrollList[chooseIndex].transform.FindChild("03").gameObject.SetActive(true);
				if( stageScrollList[chooseIndex].transform.FindChild("04") != null )
					stageScrollList[chooseIndex].transform.FindChild("04").gameObject.SetActive(false);
			}
			else
			{
				if( stageScrollList[chooseIndex].transform.FindChild("01") != null )
					stageScrollList[chooseIndex].transform.FindChild("01").gameObject.SetActive(true);
				if( stageScrollList[chooseIndex].transform.FindChild("02") != null )
					stageScrollList[chooseIndex].transform.FindChild("02").gameObject.SetActive(false);
			}
		}

		if( chooseIndex == lastFinishedStageIndex )
		{
			if( chooseIndex < 9 )
			{
				if( stageScrollList[chooseIndex].transform.FindChild("01") != null )
					stageScrollList[chooseIndex].transform.FindChild("01").gameObject.SetActive(false);
				if( stageScrollList[chooseIndex].transform.FindChild("02") != null )
					stageScrollList[chooseIndex].transform.FindChild("02").gameObject.SetActive(false);
				if( stageScrollList[chooseIndex].transform.FindChild("03") != null )
					stageScrollList[chooseIndex].transform.FindChild("03").gameObject.SetActive(false);
				if( stageScrollList[chooseIndex].transform.FindChild("04") != null )
					stageScrollList[chooseIndex].transform.FindChild("04").gameObject.SetActive(true);
			}
			else
			{
				if( stageScrollList[chooseIndex].transform.FindChild("01") != null )
					stageScrollList[chooseIndex].transform.FindChild("01").gameObject.SetActive(true);
				if( stageScrollList[chooseIndex].transform.FindChild("02") != null )
					stageScrollList[chooseIndex].transform.FindChild("02").gameObject.SetActive(false);
			}
		}

		chooseIndex = eventID;
		if( chooseIndex < 9 )
		{
			if( stageScrollList[chooseIndex].transform.FindChild("01") != null )
				stageScrollList[chooseIndex].transform.FindChild("01").gameObject.SetActive(false);
			if( stageScrollList[chooseIndex].transform.FindChild("02") != null )
				stageScrollList[chooseIndex].transform.FindChild("02").gameObject.SetActive(true);
			if( stageScrollList[chooseIndex].transform.FindChild("03") != null )
				stageScrollList[chooseIndex].transform.FindChild("03").gameObject.SetActive(false);
			if( stageScrollList[chooseIndex].transform.FindChild("04") != null )
				stageScrollList[chooseIndex].transform.FindChild("04").gameObject.SetActive(false);
		}
		else
		{
			if( stageScrollList[chooseIndex].transform.FindChild("01") != null )
				stageScrollList[chooseIndex].transform.FindChild("01").gameObject.SetActive(false);
			if( stageScrollList[chooseIndex].transform.FindChild("02") != null )
				stageScrollList[chooseIndex].transform.FindChild("02").gameObject.SetActive(true);
		}

//		if( chooseIndex < 4 ) 
//			stageScrollList[0].transform.parent.parent.gameObject.GetComponent<UIScrollView>().contentPivot = UIWidget.Pivot.Top;
//		else if( lastFinishedStageIndex > 4 && lastFinishedStageIndex < 7 ) 
//			stageScrollList[0].transform.parent.parent.gameObject.GetComponent<UIScrollView>().contentPivot = UIWidget.Pivot.Center;
//		else
//			stageScrollList[0].transform.parent.parent.gameObject.GetComponent<UIScrollView>().contentPivot = UIWidget.Pivot.Bottom;
//		
//		stageScrollList[0].transform.parent.parent.gameObject.GetComponent<UIScrollView>().ResetPosition();

		MoveToStagePanel();
	}

	bool isDrag = false;
	public void MoveToStagePanel(bool isRight)
	{
		int dragChooseIndex = 0;
		if( !isDrag )
		{
			if( isRight )
			{
				if( chooseIndex+1 >= lastFinishedStageIndex + 1 )
					return;
				else
					dragChooseIndex = chooseIndex+1;
			}
			else
			{
				if( chooseIndex-1 >= 0 )
					dragChooseIndex = chooseIndex-1;
				else
					return;
			}

			OnEvent(dragChooseIndex);

			isDrag = true;
		}
	}

	public void MoveToStagePanel()
	{
        expansionWarStartButtonObject.SetActive(false);

		if( Mathf.Abs(chooseIndex-currentIndex) > 1 )
		{
		}
		if( currentIndex < 9 )
		{
			if( currentIndex < lastFinishedStageIndex )
			{
				stages[currentIndex].transform.FindChild((currentIndex+1)+"_1").gameObject.SetActive(true);
				stages[currentIndex].transform.FindChild((currentIndex+1)+"_2").gameObject.SetActive(false);
			}
		}
		
		
		for(int i=0; i<=(chooseIndex>currentIndex?chooseIndex:currentIndex); i++)
		{
			tweenRotationList[i] = TweenRotation.Begin(stages[i], 0.5f, Quaternion.Euler(new Vector3(0f, 0f, 0f)));
			tweenRotationList[i].from = new Vector3(0f, 42f, 0f);
//			if( i<9 )
//			{
//				stages[i].transform.FindChild((i+1)+"_1").GetComponent<BlurController>().BlurAmount = 0f;
//				stages[i].transform.FindChild((i+1)+"_2").GetComponent<BlurController>().BlurAmount = 0f;
//			}
		}
		if( chooseIndex > currentIndex )
		{
            if (chooseIndex < 9)
            {
                Vector3 pos = stages[chooseIndex + 1].transform.localPosition;
                pos.z = (chooseIndex + 2) * -600f;
                stages[chooseIndex + 1].transform.localPosition = pos;

                tweenPosionList[currentIndex + 1] = TweenPosition.Begin(stages[currentIndex + 1], 0.25f, new Vector3(stages[currentIndex + 1].transform.localPosition.x, stages[currentIndex + 1].transform.localPosition.y, (currentIndex + 1) * -600f));
                tweenPosionList[currentIndex + 1].delay = 0.2f;
            }
            else
            {
                tweenPosionList[currentIndex + 1] = TweenPosition.Begin(stages[currentIndex + 1], 0.25f, new Vector3(stages[currentIndex + 1].transform.localPosition.x, stages[currentIndex + 1].transform.localPosition.y, (currentIndex + 1) * -600f));
                tweenPosionList[currentIndex + 1].delay = 0.2f;
            }
//			Vector3 pos = stages[chooseIndex+1].transform.localPosition;
//			pos.z = (chooseIndex+2)*-500f;
//			stages[chooseIndex+1].transform.localPosition = pos;
		}
		else
		{
			TweenAlpha tweenAlpha = TweenAlpha.Begin(stages[currentIndex], 0.4f, 0f);
			tweenAlpha.delay = 0.5f;
		}
		tweenPositionRoot = TweenPosition.Begin(stageRoot[selectedAreaIndex-1], 0.5f, new Vector3(stageRoot[selectedAreaIndex-1].transform.localPosition.x, stageRoot[selectedAreaIndex-1].transform.localPosition.y, 900f+(chooseIndex*600f)));
		EventDelegate.Add(tweenPositionRoot.onFinished, OnFinishedMoveStageRoot);
		tweenPositionRoot.delay = 0.5f;
	}

	void OnFinishedMoveStageRoot()
	{
		if( chooseIndex < 9 )
		{
			Vector3 pos = stages[chooseIndex+1].transform.localPosition;
			pos.z = (chooseIndex+2)*-600f;
			stages[chooseIndex+1].transform.localPosition = pos;
		}

		for(int i=0; i<=chooseIndex; i++)
		{
			Debug.LogError("Rotate!");
			tweenRotationList[i] = TweenRotation.Begin(stages[i], 0.5f, Quaternion.Euler(new Vector3(0f, 42f, 0f)));
			tweenRotationList[i].from = new Vector3(0f, 0f, 0f);
		}

		for(int i=0; i<chooseIndex; i++)
		{
			stages[i].transform.FindChild((i+1)+"_1").gameObject.SetActive(true);
			stages[i].transform.FindChild((i+1)+"_2").gameObject.SetActive(false);
//			stages[i].transform.FindChild((i+1)+"_1").GetComponent<BlurController>().BlurAmount = 1f;
//			stages[i].transform.FindChild((i+1)+"_2").GetComponent<BlurController>().BlurAmount = 1f;
		}

		if( chooseIndex < 9 )
		{
			stages[chooseIndex].transform.FindChild((chooseIndex+1)+"_1").gameObject.SetActive(false);
			stages[chooseIndex].transform.FindChild((chooseIndex+1)+"_2").gameObject.SetActive(true);
		}

		if( chooseIndex < currentIndex && currentIndex < 9 )
		{
			Vector3 pos = stages[currentIndex+1].transform.localPosition;
			pos.z = (currentIndex+1)*-600f;
			stages[currentIndex+1].transform.localPosition = pos;
		}

		UIRect mRect = stages[currentIndex].GetComponent<UIRect>();
		if( mRect == null ) mRect = stages[currentIndex].GetComponentInChildren<UIRect>();
		mRect.alpha = 1f;

//		SpringPosition.Begin(stageRoot[selectedAreaIndex-1], stageRoot[selectedAreaIndex-1].transform.localPosition - new Vector3(0f,0f,0.5f), 10f);

		currentIndex = chooseIndex;

        if (currentIndex >= lastFinishedStageIndex)
            expansionWarStartButtonObject.SetActive(true);

		isDrag = false;
	}

	public static bool isExpansionWar = false;
	public void OnClickedStartBattle()
	{
		StandbyScene.startMode = BattleStartMode.ExpansionWar;
        SceneController.LoadLevel("Lobby_4vs4");
	}
    	

	public void OnDragStage()
	{
	}
}
